/* 
 * File:   main.cpp
 * Author: mvdr
 *
 * Created on March 19, 2013, 2:55 PM
 */


#include <cstdlib>
#include <iostream>
#include <fstream>
#include "Model.h"
#include "Game.h"
#include "Utilities.h"
#include <cstring>
#include <ctime>
#include <sstream>


using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    set<string> arguments;
    
    
    
    int argOffset = 1;
    if (argc > 1)
        if (('0' <= argv[1][0]) & (argv[1][0] <= '9'))
            argOffset = 2;
    
    for (int i = argOffset; i < argc; ++i)
        arguments.insert(argv[i]);
    
    bool magic = arguments.count("-m") == 1 || arguments.count("--magic") == 1;        
    bool verbose = arguments.count("-v") == 1 || arguments.count("--verbose") == 1;    
    bool kooi = arguments.count("-k") == 1 || arguments.count("--kooi") == 1;
    bool inf = arguments.count("-i") == 1 || arguments.count("--infinite") == 1;
    bool full = arguments.count("-f") == 1 || arguments.count("--full") == 1;
    bool count = arguments.count("-c") == 1 || arguments.count("--count") == 1;
    
    string filename = "no-magic";
    
    if (magic)
    {
    	cout << "Using epistemagic.\n";
        filename = "magic";
    }
    else
    	cout << "No epistemagic.\n";
    

    
    time_t now = time(0);
    tm *ltm = localtime(&now);    
    
    stringstream ss;
    ss << "results/" << filename;
    if (kooi)
    {
        cout << "Kooi\'s choice.\n";
        ss << "_kooi";
    }
    
    if (inf)
        ss << "_inf";
    if (full)
        ss << "_full";
    
    ss << '_' << prettyString(ltm->tm_mday, 2)
            << '-' << prettyString(ltm->tm_mon + 1, 2)
            << '_' << prettyString(ltm->tm_hour, 2)
            << ':' << prettyString(ltm->tm_min, 2) << ".csv";
    
    filename = ss.str();
    
    if (verbose)
        cout << "So you want verbosity, \'eh?\n";
    
    cout << "Results are written to " << filename << '\n';
    
    Game game(magic, inf, kooi, full, verbose, count);
    int runs = argOffset == 2 ? atoi(argv[1]) : game.nStates();
    int digits = runs < 10 ? 1 : runs < 100 ? 2 : runs < 1000 ? 3 : 4;
    
    
    for (int i = 0; i != runs; ++i)
    {
        if (not verbose)
            cout << "\rGAME " << prettyString(i + 1, digits) << " / "
                 << runs;        
        vector<size_t> result = game.run();
        writeResult(result, magic, filename);
    }
    cout << '\n';
}



